/**
 * 
 */
package org.jboss.minstaller

import java.io.File; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 



/**
 * @author jjeon@redhat.com
 *
 */
public class ServerConfig {
    def xmldoc;
    def variables;
    def servers;
    
    public ServerConfig(filename) {
        readConfig(filename);
    }
    
    void readConfig(filename) {
        println "    >>> config filename=" + filename;
        def config = new XmlSlurper().parse( new File(filename));
        
        variables = config.variables;
        servers = config.servers;
    }
    
    public String getWorkingDir() {
        def workingDir = variables.workingdir.text();
        return workingDir;
    }
    
    public String getJBossVersion() {
        def version = variables.jboss.version.text();
        println "   >>> JBoss Version=" + version;
        return version;
    }
    
    public String getServerType() {
        def server = variables.jboss.server.text();
        
        println "   >>> JBoss ServerType=" + server;
        return server;
    }
    
    public String getLocalServerType() {
        def localdir = variables.jboss.localdir.text();
        
        println "   >>> JBoss Local ServerType=" + localdir;
        return localdir;
    }    
    
    public String getWgethost() {
        def wgethost = variables.jboss.wgethost.text();
        
        println "   >>> JB29 WGET HOST=" + wgethost;
        return wgethost;
    }    
    
    public String getWgetport() {
        def wgetport = variables.jboss.wgetport.text();
        
        println "   >>> JB29 WGET PORT=" + wgetport;
        return wgetport;        
    }
    
    public String getWgethostPort() {
        return variables.jboss.wgethost.text() + ":" + variables.jboss.wgetport.text();
    }
    
    /**
     * TODO : get application lists
     */
    public List getApplications() {
        return new ArrayList();
    }
    
    public String getApplication() {
        def application = variables.jboss.applications.name.text();
        return application;
    }
    
    public String getServerUserHome() {
        def userhome = variables.server.userhome.text();
        return userhome;
    }
    
    public String getJBossInstallDir() {
        def jbossDir = variables.server.jbossinstalldir.text();
        return jbossDir;
    }
    
    public String getServerJBossHome() {
        def jbosshome = variables.server.jbosshome.text();
        return jbosshome;
    }
    
    public String getServerServerHome() {
        def serverhome = variables.server.serverhome.text();
        return serverhome;
    }
    
    public String getServerUserid() {
        def userid = variables.server.userid.text();
        return userid;
    }
    
    public String getServerPasswd() {
        def passwd = variables.server.passwd.text();
        return passwd;
    }
    
    public String getAppHome() {
        def apphome = variables.server.apphome.text();
        return apphome;
    }
    
    public int getServersSize() {
        int size = servers.server.size();
        println "> Number of physical server=" + size;
        return size;
    }
    
    public int getJBossServersSize(def server) {
        int size = server.jbossservers.jbossserver.size();
        println "> Number of jboss instances=" + size;
        return size;
    }
    
    
    public String getServerIp() {
        def serverip = servers.server.ip.text();
        return serverip;
    }
    
    /*
     <server>
     <name>localhost</name>
     <ip>127.0.0.1</ip>
     <jbossservers>
     <jbossserver>
     <servername>pmSrv11</servername>
     <paritionname>pmCluster</paritionname>
     <multicastaddr>228.1.1.1</multicastaddr>
     <JBM_DC_PORT>11011</JBM_DC_PORT>
     <JBM_CC_PORT>11021</JBM_CC_PORT>
     <port_binding_name>ports-01</port_binding_name>
     <jnp-port>1199</jnp-port>
     <JBM_SERVERPEERID>0</JBM_SERVERPEERID>
     <adminuserid>pmadmin</adminuserid>
     <adminuserpasswd>!pM1234%^</adminuserpasswd>            
     </jbossserver>
     <jbossserver>
     <servername>pmSrv12</servername>
     <paritionname>pmCluster</paritionname>
     <multicastaddr>228.1.1.1</multicastaddr>
     <JBM_DC_PORT>11011</JBM_DC_PORT>
     <JBM_CC_PORT>11021</JBM_CC_PORT>
     <port_binding_name>ports-02</port_binding_name>
     <jnp-port>1299</jnp-port>
     <JBM_SERVERPEERID>1</JBM_SERVERPEERID>
     <adminuserid>pmadmin</adminuserid>
     <adminuserpasswd>!pM1234%^</adminuserpasswd>            
     </jbossserver>            
     </jbossservers>
     </server>
     */
    
    static void main(args) {
        ServerConfig serverConfig = new ServerConfig();
        serverConfig.getWorkingDir();
        serverConfig.getServersSize();
        serverConfig.getJBossServersSize(serverConfig.servers.server[0]);
        serverConfig.getJBossServersSize(serverConfig.servers.server[1]);
    }
}
